package org.webautotest.reservation;





import org.openqa.selenium.By;

import cn.bmyi.webautotest.aimee.BrowserEmulator;
import cn.bmyi.webautotest.aimee.BrowserEmulator.Element;

/**
 * 封装体检预约登记体检卡页面元素和操作
 * @author gaowen
 *
 */
public class RigistCardPage
{
	public BrowserEmulator browser;
	public String url;
	public static class PageElement
	{
		public static final String SEARCH_TITILE_XPATH="/html/body/div/header/div[1]";//标题
		public static final String SEARCH_TAB1_XPATH="/html/body/div/header/div[2]/a[1]";//导航栏-体检卡登录
		public static final String SEARCH_TAB2_XPATH="/html/body/div/header/div[2]/a[2]";//导航栏-填写信息
		public static final String SEARCH_TAB3_XPATH="/html/body/div/header/div[2]/a[3]";//导航栏-选择套餐
		public static final String SEARCH_TAB4_XPATH="/html/body/div/header/div[2]/a[4]";//导航栏-选择机构
		public static final String SEARCH_ACCOUNTINPUT_ID="cardNum";//体检卡号输入框
		public static final String SEARCH_PASSWORDINPUT_ID="cardPwd";//密码输入框
		public static final String SEARCH_SARTBUTTON_XPATH="/html/body/div/div[1]/div/a";//开始预约按钮区域
		public static final String SEARCH_POPBOX_XPATH="/html/body/div[3]";//弹窗
		public static final String SEARCH_POPTIT_XPATH="/html/body/div[3]/div[1]";//弹窗标题
		public static final String SEARCH_POPCONT_XPATH="/html/body/div[3]/div[2]";//弹窗提示文字
		public static final String SEARCH_POPBTN_XPATH="/html/body/div[3]/div[3]/a";//弹窗提示文字
		public static final String SEARCH_LOGO_XPATH="/html/body/div/header/img";//logo
		public static final String SEARCH_CARDNAME_XPATH="/html/body/div/div[1]/dl/dd/h3";//
		public static final String SEARCH_CARDNO_XPATH="/html/body/div/div[1]/dl/dd/p";//
		public static final String SEARCH_CARDBUTTON_XPATH="/html/body/div/div[1]/dl/dd/a";//
	}
	public RigistCardPage(BrowserEmulator browser,String url)
	{
		this.browser=browser;
		this.url=url;
		this.browser.open(url);
	}
	
	/**
	 * 输入卡号
	 * @param cardNum 卡号
	 */
	public void inputCardNo(CharSequence...cardNum)
	{
		browser.findElement("id", PageElement.SEARCH_ACCOUNTINPUT_ID).type(cardNum);
	}
	
	/**
	 * 输入密码
	 * @param passWord 密码
	 */
	public void inputPassWord(CharSequence...passWord)
	{
		browser.findElement("id", PageElement.SEARCH_PASSWORDINPUT_ID).type(passWord);
	}
	
	/**
	 * 点击开始预约按钮
	 */
	public void clickStartButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_SARTBUTTON_XPATH).click();
	}
	
	/**
	 * 获取开始预约按钮属性
	 * @return 属性
	 */
	public String getStartButtonAttribute()
	{
		return browser.findElement("xpath", PageElement.SEARCH_SARTBUTTON_XPATH).getAttribute("class");
	}
	
	/**
	 * 检查是否弹窗
	 * @return true：弹窗了；false：没有弹窗
	 */
	public boolean isPopBox()
	{
		try
		{
			browser.findElement("xpath", PageElement.SEARCH_POPBOX_XPATH);
			return true;
		}
		catch (Exception e)
		{
			return false;
		}		
	}
	
	/**
	 * 检查弹窗标题
	 * @return 弹窗标题
	 */
	public String getPopTitle()
	{
		return browser.findElement("xpath", PageElement.SEARCH_POPTIT_XPATH).getText();
	}
	
	/**
	 * 检查弹窗提示内容
	 * @return 弹窗提示内容
	 */
	public String getPopContent()
	{
		return browser.findElement("xpath", PageElement.SEARCH_POPCONT_XPATH).getText();
	}
	
	/**
	 * 点击弹窗确认按钮
	 */
	public void clickPopButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_POPBTN_XPATH).click();
	}
	
	/**检查标题名
	 * @return 标题名
	 * 
	 */
	public String getTitle()
	{
		By locator=By.xpath(PageElement.SEARCH_TITILE_XPATH);
		Element married=browser.new Element(locator);
		married.waitUntilDisplay();
		return browser.findElement("xpath", PageElement.SEARCH_TITILE_XPATH).getText();
	}
	
	/**
	 * 检查第1个tab名
	 * @return 第1个tab名
	 */
	public String getTab1Name()
	{
		return browser.findElement("xpath", PageElement.SEARCH_TAB1_XPATH).getText();
	}
	
	/**
	 * 检查第2个tab名
	 * @return 第2个tab名
	 */
	public String getTab2Name()
	{
		return browser.findElement("xpath", PageElement.SEARCH_TAB2_XPATH).getText();
	}
	
	/**
	 * 检查第3个tab名
	 * @return 第3个tab名
	 */
	public String getTab3Name()
	{
		return browser.findElement("xpath", PageElement.SEARCH_TAB3_XPATH).getText();
	}
	
	/**
	 * 检查第4个tab名
	 * @return 第4个tab名
	 */
	public String getTab4Name()
	{
		return browser.findElement("xpath", PageElement.SEARCH_TAB4_XPATH).getText();
	}
	
	/**
	 * 检查logo是否显示
	 * @return true：显示；false：不显示
	 */
	public boolean isLogeDisPlay()
	{
		try 
		{
			browser.findElement("xpath", PageElement.SEARCH_LOGO_XPATH);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 等待Logo消失
	 */
	public void waiteLogoDespear()
	{
		By locator =By.xpath(PageElement.SEARCH_LOGO_XPATH);
		Element logo=browser.new Element(locator);
		logo.waitUntilConceal();
	}
	
	/**
	 * 输入卡号密码点击开始预约
	 * @param cardNo 卡号
	 * @param passWord 密码
	 */
	public void startReservation(String cardNo,String passWord)
	{
		this.inputCardNo(cardNo);
		this.inputPassWord(passWord);
		this.clickStartButton();
		By locator=By.xpath(PageElement.SEARCH_SARTBUTTON_XPATH);
		Element married=browser.new Element(locator);
		married.waitUntilConceal();
		
	}
	
	/**
	 * 获取已绑定的体检卡名字
	 * @return 体检卡名字
	 */
	public String getCardName()
	{
		return browser.findElement("xpath", PageElement.SEARCH_CARDNAME_XPATH).getText();
	}
	
	/**
	 * 获取已绑定的体检卡卡号
	 * @return 体检卡卡号
	 */
	public String getCardNo()
	{
		By locator=By.xpath(PageElement.SEARCH_CARDNO_XPATH);
		Element married=browser.new Element(locator);
		married.waitUntilDisplay();
		return browser.findElement("xpath", PageElement.SEARCH_CARDNO_XPATH).getText();
	}
	
	/**
	 * 等待开始预约按钮消失
	 */
	public void waitStartButtonDespear()
	{
		By locator=By.xpath(PageElement.SEARCH_SARTBUTTON_XPATH);
		Element married=browser.new Element(locator);
		married.waitUntilConceal();
	}
	
	/**
	 * 点击已绑定体检卡的预约按钮
	 */
	public void clickCardButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_CARDBUTTON_XPATH).click();
	}
	
	/**
	 * 清空卡号输入框内容
	 */
	public void clearAccountInput()
	{
		browser.findElement("id", PageElement.SEARCH_ACCOUNTINPUT_ID).clear();
	}
	
	/**
	 * 清空密码输入框内容
	 */
	public void clearPassWordInput()
	{
		browser.findElement("id", PageElement.SEARCH_PASSWORDINPUT_ID).clear();
	}
	
	/**
	 * 检查卡是否被使用
	 * @param cardNo
	 * @param passWord
	 * @return true：被使用（该卡号密码不可使用）;false：该体检卡可用
	 * @throws InterruptedException 
	 */
	public boolean checkCardIsUsed(String cardNo,String passWord) throws InterruptedException
	{
		String url=browser.getUrl();
		this.inputCardNo(cardNo);
		this.inputPassWord(passWord);
		this.clickStartButton();
		try
		{
			this.waitStartButtonDespear();
		}
		catch (Exception e)
		{
			return false;
		}
//		Thread.sleep(1000);
		String afterurl=browser.getUrl();
		return url.equals(afterurl);
	}
}
